
package com.sun.showcase.biz.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.mall.dao.DistributionDao;
import com.sun.showcase.biz.mall.service.DistributionService;
import com.sun.showcase.client.domain.mall.Distribution;
import com.sun.showcase.client.query.mall.DistributionQuery;
import com.sun.showcase.exception.BusinessException;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.ArithUtil;
import com.sun.showcase.utils.UuidUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("distributionService")
@Transactional
public class DistributionServiceImpl implements DistributionService{
	@Autowired
	private DistributionDao distributionDao;
	
	public void setDistributionDao(DistributionDao dao) {
		this.distributionDao = dao;
	}

	public DataGrid datagrid(DistributionQuery distributionQuery) {
		DataGrid j = new DataGrid();
		Pager<Distribution> pager  = find(distributionQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<DistributionQuery> getQuerysFromEntitys(List<Distribution> distributions) {
		List<DistributionQuery> distributionQuerys = new ArrayList<DistributionQuery>();
		if (distributions != null && distributions.size() > 0) {
			for (Distribution tb : distributions) {
				DistributionQuery b = new DistributionQuery();
				BeanUtils.copyProperties(tb, b);
				distributionQuerys.add(b);
			}
		}
		return distributionQuerys;
	}

	private Pager<Distribution> find(DistributionQuery distributionQuery) {
		Page<Distribution> page = PageHelper.startPage(distributionQuery.getPage().intValue(), distributionQuery.getRows().intValue());
		List<Distribution> list = distributionDao.findList(distributionQuery);
		Pager<Distribution> pager = new Pager<Distribution>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public Distribution add(DistributionQuery distributionQuery) {
		DistributionQuery q = new DistributionQuery();
		q.setGoodsId(distributionQuery.getGoodsId());
		q.setLevel(distributionQuery.getLevel());
		List<DistributionQuery> list =  this.listAll(q);
		if(list.size() != 0) {
			throw new BusinessException("分销层级重复");
		}
		
		Distribution t = new Distribution();
		BeanUtils.copyProperties(distributionQuery, t);
		t.setCreateDate(new Date());
	    t.setId(UuidUtil.get32UUID());
		distributionDao.save(t);
		distributionQuery.setId(t.getId());
		
		q.setLevel(null);
		list =  this.listAll(q);
		Double totalRate = 0D;
		for(DistributionQuery d : list) {
			totalRate = ArithUtil.add(totalRate, d.getRate());
		}
		if(totalRate>1) {
			throw new BusinessException("同一个商品，总比例比例不能大于1");
		}
		return t;
		
	}

	public void update(DistributionQuery distributionQuery) {
		Distribution t = distributionDao.getById(distributionQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(distributionQuery, t);
	    	distributionDao.update(t);
	    	
	    	DistributionQuery q = new DistributionQuery();
			q.setGoodsId(distributionQuery.getGoodsId());
	    	q.setLevel(null);
	    	List<DistributionQuery> list =  this.listAll(q);
			Double totalRate = 0D;
			for(DistributionQuery d : list) {
				totalRate = ArithUtil.add(totalRate, d.getRate());
			}
			if(totalRate>1) {
				throw new BusinessException("同一个商品，总比例比例不能大于1");
			}
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(DistributionQuery distributionQuery) {
	    distributionDao.updatePart(distributionQuery);
	}

	public void delete(String[] ids) {
		distributionDao.deleteAc(ids);
	}

	//逻辑删除 更新状态位
	public void deletePt(String[] ids) {
		distributionDao.deletePt(ids);
	}

	public Distribution get(DistributionQuery distributionQuery) {
		return distributionDao.getById(distributionQuery.getId());
	}

	public Distribution get(String id) {
		return distributionDao.getById(id);
	}

	
	public List<DistributionQuery> listAll(DistributionQuery distributionQuery) {
	    List<Distribution> list = distributionDao.findList(distributionQuery);
		List<DistributionQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}

	@Override
	public int getNextLevel(String goodsId) {
		DistributionQuery q = new DistributionQuery();
		q.setGoodsId(goodsId);
		q.setOrderColum("level");
		q.setOrderRule("desc");
		List<DistributionQuery> list = this.listAll(q);
		if(list.size()==0) {
			return 1;
		}
		return list.get(0).getLevel()+1;
	}
	
	
}
